//--------------------------------------------------------------------------------
//
//  Written by: Tyson Bailey
//
//  Date: 2/17/2010
//
//  Purpose: General purpose Emulator for Power PC 403 Board for UNM
//
//
//  Notes: This code is/will be released under the MIT license, please follow
//          all guidelines that it requires. Additionally if you update a file
//          please include your revision history.
// 
//          So far there is a basic setup of structure for reading the input, 
//          and determining what command is being used, if you have a better idea
//          or recommendation I'm open to suggestions. I think this would be great
//          to create a class for this and that may happen, but for now this is a 
//          basic kludging.
//
//          Commands Recognized Thus Far:
//                   con <optional>
//                   dow <file>
//                   exit
//                   help-running
//                   sh <genelf2> <file.s> <file.elf> > file.lst
//                   stp
//
//                    #  Who          Date          Email
//  Revision History: 0  Tyson Bailey 2/17/2010     onaclov2000@gmail.com 
//--------------------------------------------------------------------------------

void interpretCode(string file);
void interpret(string cmd)
{
    int i;
    string parsedCommand;
    string file;
    string location;
    if (cmd != "")
    {
        i = cmd.find(" ");
        
        parsedCommand = cmd.substr(0, i);
        if (parsedCommand == "con") 
        {
           if (i < cmd.length()) 
           {
             // Check to see if there is a parameter, if not then free running
             location = cmd.substr(i, cmd.length()-1);
             cout << "Move PC to: " << location  << " and run!" << endl;
             //setState("startFrom");
           }
           else
           {
              cout << "Free Running from where ever PC is pointing" << endl; // put ppc into free running state
              //setState("con");
           } 
        }
        else if (parsedCommand == "dow") 
        {
           if (i < cmd.length()) 
           {
                 file = cmd.substr(i, cmd.length()-1);
                 //setState();
                // passToSim(file);
           }
        }
        else if (parsedCommand == "exit") 
        {
           cout << "Exiting..." << endl;
           exit(0);
        }
        else if (parsedCommand == "help") 
        {
            cout << "XMD Terminal Commands Types:" << endl;
            cout << "init......... Load/Initialize the System Files" << endl;
            cout << "connect...... Connect to System Target" << endl;
            cout << "files........ Load ELF/Data files" << endl;
            cout << "running...... Program Execution" << endl;
            cout << "breakpoints.. Setting Breakpoints/Watchpoints" << endl;
            cout << "trace........ Tracing and Profiling options" << endl;
            cout << "misc......... Miscellaneous Options" << endl;
            cout << "help......... Help on help" << endl;
            cout << endl;
            cout << "Type \"help\" to display XMD command types" << endl;
            cout << "Type \"help\" followed by above \"type\" for more options" << endl;
        }
        else if (parsedCommand == "gen_elf2") 
        {
           file = cmd.substr(i, cmd.length()-1); 
           interpretCode(file);
        }
        else if (parsedCommand == "con") 
        {
           file = cmd.substr(i, cmd.length()-1); 
           interpretCode(file);
        }
        else if (parsedCommand == "stp") 
        {
           // allow one instruction to occur
        }
        
    }
}


void interpretCode(string file)
{

//Initially we will open file

cout << "file" << file << endl;



//Next we'll extract each line


// next we'll convert to opcode and operand.

//Mnemonic   Name  Operation                              Operand Syntax
//addi       Add   Immediate XER and CR0 are not updated. rD,rA,SIMM
//[14][rd][ra][simm]
}
